package edu.ycu.aladdin.collector.service.weibo;

import com.alibaba.dubbo.config.annotation.Service;
import edu.ycu.aladdin.collector.CrawlerConfigPool;
import edu.ycu.aladdin.collector.api.CrawlerService;
import edu.ycu.aladdin.collector.api.entity.weibo.UserInfoTaskQueue;
import edu.ycu.aladdin.collector.service.WeiboUserInfoMongoService;
import edu.ycu.aladdin.collector.utils.ParseUtils;
import edu.ycu.aladdin.common.interfaces.ProcessAble;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import us.codecraft.webmagic.Page;

/**
 * @author <a href="mailto:wangruihuano@gmail.com">王瑞环</a>
 * @since 2019-04-07 23:21
 */
@Slf4j
@org.springframework.stereotype.Service
@Service(interfaceClass = CrawlerService.class, timeout = 10000, group = "user-info-crawler-service")
public class UserInfoCrawlerService extends BaseCrawlerWebmagicService<UserInfoTaskQueue> {

    @Value("${weibo.template.user-info-url}")
    private String templateUrl;

    private final WeiboUserInfoMongoService weiboUserInfoMongoService;

    /**
     * @param crawlerConfigPool         爬虫配置池
     * @param weiboUserInfoMongoService user info mongo服务
     */
    @Autowired
    public UserInfoCrawlerService(CrawlerConfigPool crawlerConfigPool, WeiboUserInfoMongoService weiboUserInfoMongoService) {
        super(crawlerConfigPool);
        this.weiboUserInfoMongoService = weiboUserInfoMongoService;
    }

    @Override
    protected void storage(UserInfoTaskQueue task) {

        log.info("持久化任务队列:{}", task);
    }

    @Override
    protected void processException(UserInfoTaskQueue task, Exception e) {
        log.warn("执行任务:{},捕获异常:{}", task, e);
    }

    @Override
    protected String initUrl(UserInfoTaskQueue task) {
        return replateTemplateUrl(task.getTaskQueue().poll());
    }

    @Override
    protected ProcessAble<Page> doProcess(UserInfoTaskQueue task) {
        return page -> {
            String rawText = page.getRawText();
            log.info("爬取内容:{}", rawText);
            weiboUserInfoMongoService.addUser(ParseUtils.parseUserInfoFromJson(rawText), false);
            if (!task.isEmpty()){
                page.addTargetRequest(replateTemplateUrl(task.pollOne()));
            }
        };
    }

    private String replateTemplateUrl(String uid) {
        return templateUrl.replace("{{uid}}", uid);
    }

}
